package com.masterclient.app.modules.goods.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.bean.goods.ProductBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.modules.goods.newpage.activity.NewCommodityDetailActivity;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * 作者：sean
 * 创建时间：2017/6/23 16:32
 * 功能说明：
 */

public class CommonGoodsRecyclerAdapter extends BaseQuickAdapter<ProductBean, BaseViewHolder> {

    private int width;

    public CommonGoodsRecyclerAdapter() {
        super(R.layout.item_footer_good);
        this.strings = new String[]{};
        width = (int)((ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(41))/2f);
    }

    private String[] strings;
    /*
     **source_page, source_module, source_element, key_word, key_word_type
     */
    public void setStrings(String... strings) {
        this.strings = strings;
    }


    @Override
    protected void convert(BaseViewHolder helper, ProductBean item) {

        //渲染价格
        renderAllPrice(helper,item);

        TextView tvFavoriteNum =  helper.getView(R.id.tv_favorite_num);
        TextView tvBuy =  helper.getView(R.id.tv_buy);
        tvFavoriteNum.setVisibility(View.GONE);
        tvBuy.setVisibility(View.VISIBLE);

        ImageView imageView =  helper.getView(R.id.iv_goods);
        ViewTransformUtil.glideImageView(mContext,item.getSh_image(),imageView,new RoundedCornersTransformation(0, 0),
                R.mipmap.defaults,width,width);

        helper.setText(R.id.tv_sub_title,item.getSh_name());

        ImageView ivTagNew = helper.getView(R.id.iv_tag_new);

        if("1".equals(item.getSh_is_new())){
            ivTagNew.setVisibility(View.VISIBLE);
        }else {
            ivTagNew.setVisibility(View.GONE);
        }

        renderActivityImage(helper,item);

        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, NewCommodityDetailActivity.class);
                intent.putExtra("id",item.getSh_id());
                intent.putExtra("source_page", strings.length>0?strings[0]:"");
                intent.putExtra("source_module",strings.length>1?strings[1]:"");
                intent.putExtra("source_element",strings.length>2?strings[2]:"");
                intent.putExtra("key_word", strings.length>3?strings[3]:"");
                intent.putExtra("key_word_type", strings.length>4?strings[4]:"");
                intent.putExtra("source_position",helper.getAdapterPosition()-getHeaderLayoutCount()+"");
                mContext.startActivity(intent);
            }
        });
    }

    private void renderActivityImage(BaseViewHolder helper, ProductBean item) {

    }

    private void renderAllPrice(BaseViewHolder helper, ProductBean item) {
        TextView tvPrice = helper.getView(R.id.price);
        TextView tvOfficialPrice = helper.getView(R.id.tv_official_price);

        Utils.set_price_style2(tvPrice,"¥ " + item.getSh_price());
        Utils.setDin(tvPrice,mContext);
        tvOfficialPrice.setText("¥" +item.getSh_line_price());
        tvOfficialPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        tvOfficialPrice.setVisibility(View.VISIBLE);
    }
}
